#pragma once

class Texture
{
	friend class CTextureHandler;
	IDirect3DTexture9* m_pTexture;
	std::string szTexName;

	Texture(IDirect3DTexture9* pTex) : m_pTexture(pTex)
	{
		m_refCount = 1;
	}

	ui32 m_refCount;
	bool m_isADTTexture;

public:
	operator IDirect3DTexture9*()
	{
		return m_pTexture;
	}

	void Save()
	{
		::D3DXSaveTextureToFile("Texture.PNG", D3DXIFF_PNG, m_pTexture, 0);
	}

	void SetADTTexture() { m_isADTTexture = true; }

	std::string GetName() { return szTexName; }

	bool isEqual(Texture* pTex)
	{
		if(pTex == NULL)
			return false;

		return szTexName == pTex->GetName();
	}

	IDirect3DTexture9** GetTexPointer() { return &m_pTexture; }

	IDirect3DTexture9* operator->()
	{
		return m_pTexture;
	}

	Texture* DecRef()
	{
		if(m_refCount > 0)
			--m_refCount;
		if(m_refCount == 0)
		{
			while(m_pTexture && m_pTexture->Release());
			CTextureHandler::GetInstance()->Remove(this);
			return 0;
		}
		return this;
	}

	void Delete()
	{
		m_pTexture->Release();
		CTextureHandler::GetInstance()->Remove(this);
	}

	void AddRef()
	{
		++m_refCount;
	}
};